#!/usr/bin/env pwsh
# 用户行为监控问题诊断脚本

Write-Host "=== 用户行为监控问题诊断 ===" -ForegroundColor Cyan

# 1. 检查前端调用顺序
Write-Host "`n1. 检查前端调用顺序:" -ForegroundColor Yellow

$vueFile = "d:/src/DotNet/Lzfy_Refund_Service/RefundAuditWeb/src/views/RefundTaskManagement.vue"
if (Test-Path $vueFile) {
    $content = Get-Content $vueFile -Raw
    
    # 检查showContactRecord方法中的调用顺序
    if ($content -match "showContactRecord.*?{(.*?)}" -and $matches[1] -match "trackViewContact.*?startSession") {
        Write-Host "  ✓ 调用顺序正确: trackViewContact -> startSession" -ForegroundColor Green
    } elseif ($content -match "showContactRecord.*?{(.*?)}" -and $matches[1] -match "startSession.*?trackViewContact") {
        Write-Host "  ⚠ 调用顺序可能有问题: startSession -> trackViewContact" -ForegroundColor Yellow
        Write-Host "    建议: 应该先调用startSession，再调用trackViewContact" -ForegroundColor Gray
    } else {
        Write-Host "  ✗ 无法确定调用顺序" -ForegroundColor Red
    }
} else {
    Write-Host "  ✗ Vue文件不存在" -ForegroundColor Red
}

# 2. 检查后端ActionTypes常量
Write-Host "`n2. 检查后端ActionTypes常量:" -ForegroundColor Yellow

$modelsFile = "d:/src/DotNet/Lzfy_Refund_Service/Models/UserBehaviorModels.cs"
if (Test-Path $modelsFile) {
    $content = Get-Content $modelsFile -Raw
    
    $actionTypes = @("VIEW_PHONE", "INPUT_NOTES", "SELECT_RESULT", "SAVE_CONTACT")
    foreach ($actionType in $actionTypes) {
        if ($content -match "public const string $actionType") {
            Write-Host "  ✓ $actionType 常量已定义" -ForegroundColor Green
        } else {
            Write-Host "  ✗ $actionType 常量未定义" -ForegroundColor Red
        }
    }
} else {
    Write-Host "  ✗ Models文件不存在" -ForegroundColor Red
}

# 3. 检查后端IsKeyAction方法
Write-Host "`n3. 检查后端IsKeyAction方法:" -ForegroundColor Yellow

$serviceFile = "d:/src/DotNet/Lzfy_Refund_Service/Services/UserBehaviorService.cs"
if (Test-Path $serviceFile) {
    $content = Get-Content $serviceFile -Raw
    
    if ($content -match "IsKeyAction.*?{(.*?)}" -and $matches[1] -match "VIEW_PHONE.*?true") {
        Write-Host "  ✓ VIEW_PHONE 被标记为关键操作" -ForegroundColor Green
    } else {
        Write-Host "  ✗ VIEW_PHONE 未被标记为关键操作" -ForegroundColor Red
    }
    
    if ($content -match "UpdateOperationSessionAsync.*?VIEW_PHONE.*?PhoneViewTime") {
        Write-Host "  ✓ VIEW_PHONE 操作会更新PhoneViewTime" -ForegroundColor Green
    } else {
        Write-Host "  ✗ VIEW_PHONE 操作未更新PhoneViewTime" -ForegroundColor Red
    }
} else {
    Write-Host "  ✗ Service文件不存在" -ForegroundColor Red
}

# 4. 检查数据库表结构
Write-Host "`n4. 检查数据库表结构:" -ForegroundColor Yellow

$sqlFile = "d:/src/DotNet/Lzfy_Refund_Service/Database/用户行为监控表.sql"
if (Test-Path $sqlFile) {
    $content = Get-Content $sqlFile -Raw
    
    $fields = @("phone_view_time", "first_input_time", "last_input_time", "save_time", "total_duration", "phone_to_save_duration", "input_duration")
    foreach ($field in $fields) {
        if ($content -match "\[$field\]") {
            Write-Host "  ✓ $field 字段已定义" -ForegroundColor Green
        } else {
            Write-Host "  ✗ $field 字段未定义" -ForegroundColor Red
        }
    }
} else {
    Write-Host "  ✗ SQL文件不存在" -ForegroundColor Red
}

# 5. 生成修复建议
Write-Host "`n=== 修复建议 ===" -ForegroundColor Cyan

Write-Host "`n问题分析:" -ForegroundColor Yellow
Write-Host "1. 前端调用顺序问题: trackViewContact在startSession之前调用" -ForegroundColor Gray
Write-Host "2. 会话可能还未建立就开始跟踪行为" -ForegroundColor Gray
Write-Host "3. 需要确保会话建立后再跟踪VIEW_PHONE行为" -ForegroundColor Gray

Write-Host "`n建议修复步骤:" -ForegroundColor Yellow
Write-Host "1. 修改前端showContactRecord方法的调用顺序" -ForegroundColor Gray
Write-Host "2. 确保startSession成功后再调用trackViewContact" -ForegroundColor Gray
Write-Host "3. 添加错误处理和重试机制" -ForegroundColor Gray
Write-Host "4. 添加调试日志以跟踪问题" -ForegroundColor Gray

Write-Host "`n=== 诊断完成 ===" -ForegroundColor Cyan

# 等待用户按键退出
Read-Host "按任意键退出"